package com.yanqu.road.logic.pb;

import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.rank.UnionActivityRank;
import com.yanqu.road.entity.rank.UnionRank;
import com.yanqu.road.entity.rank.UnionValueRank;
import com.yanqu.road.entity.rank.UserRank;
import com.yanqu.road.entity.rank.stronghold.CrossActivityUnionRank;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.pb.rank.UnionActivityRankProto;
import com.yanqu.road.pb.union.UnionProto;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class RankPb {

    public static RankProto.RankTempMsg.Builder parseRankTempMsg(UserRank userRank){
        if(null == userRank){
            return null;
        }
        if(0 == userRank.getUserId()){
            return null;
        }
        RankProto.RankTempMsg.Builder msg = RankProto.RankTempMsg.newBuilder();
        msg.setUserId(userRank.getUserId());
        PlayerProto.PlayerBaseTempMsg.Builder baseMsg = PlayerBasePb.parsePlayerBaseTempMsg(userRank.getUserBaseInfo());
        if(null != baseMsg) {
            msg.setPlayerBaseData(baseMsg);
        }
        msg.setValue(userRank.getValue().toString());
        msg.setParam(userRank.getParam());
        return msg;
    }

    public static RankProto.RankListMsg.Builder parseRankListMsg(List<UserRank> userRankList){
        RankProto.RankListMsg.Builder listMsg = RankProto.RankListMsg.newBuilder();
        if(null != userRankList){
            for(UserRank userRank : userRankList){
                if (userRank.isDelete()) {
                    continue;
                }
                RankProto.RankTempMsg.Builder msg = parseRankTempMsg(userRank);
                if(null != msg){
                    listMsg.addRankList(msg);
                }
            }
        }
        return listMsg;
    }

    public static RankProto.UnionRankTempMsg.Builder parseUnionRankTempMsg(UnionRank unionRank) {
        if (null == unionRank) {
            return null;
        }
        RankProto.UnionRankTempMsg.Builder msg = RankProto.UnionRankTempMsg.newBuilder();
        msg.setUnionUid(unionRank.getUnionUid());
        UnionProto.UnionBaseTempMsg.Builder unionMsg = UnionBasePb.parseUnionBaseTempMsg(unionRank.getUnionBaseInfo());
        msg.setUnionBaseData(unionMsg);
        if (!unionRank.getGradeLimitAmountMap().isEmpty()) {
            for (Map.Entry<Integer, Integer> entry : unionRank.getGradeLimitAmountMap().entrySet()) {
                int gradeId = entry.getKey();
                int numHave = unionRank.getNowGradeAmountMap().getOrDefault(gradeId, 0);
                UnionProto.UnionMemberRankGradeTemp.Builder limitMsg = UnionProto.UnionMemberRankGradeTemp.newBuilder();
                limitMsg.setGradeId(gradeId);
                limitMsg.setNumLmit(entry.getValue());
                limitMsg.setNumHave(numHave);
                msg.addRankGrade(limitMsg);

                int originalNumHave = unionRank.getOriginalGradeAmountMap().getOrDefault(gradeId, 0);
                if (originalNumHave > 0) {
                    UnionProto.UnionRankGradeJoinLimitTemp.Builder gradeAmountMsg = UnionProto.UnionRankGradeJoinLimitTemp.newBuilder();
                    gradeAmountMsg.setGradeId(gradeId);
                    gradeAmountMsg.setNum(originalNumHave);
                    msg.addOriginalGradeAmount(gradeAmountMsg);
                }
            }
        }
        return msg;
    }

    public static RankProto.UnionRankListMsg.Builder parseUnionRankListMsg(List<UnionRank> unionRankList){
        RankProto.UnionRankListMsg.Builder listMsg = RankProto.UnionRankListMsg.newBuilder();
        if(null != unionRankList){
            for(UnionRank unionRank : unionRankList){
                RankProto.UnionRankTempMsg.Builder msg = parseUnionRankTempMsg(unionRank);
                if(null != msg){
                    listMsg.addRankList(msg);
                }
            }
        }
        return listMsg;
    }

    public static RankProto.UnionRankTempMsg.Builder parseUnionRankTempMsg(UnionValueRank unionRank){
        if(null == unionRank){
            return null;
        }
        RankProto.UnionRankTempMsg.Builder msg = RankProto.UnionRankTempMsg.newBuilder();
        msg.setUnionUid(unionRank.getUnionUid());
        UnionProto.UnionBaseTempMsg.Builder unionMsg = UnionBasePb.parseUnionBaseTempMsg(unionRank.getUnionBaseInfo());
        msg.setUnionBaseData(unionMsg);
        msg.setValue(unionRank.getValue().toString());
        if (!unionRank.getGradeLimitAmountMap().isEmpty()) {
            for (Map.Entry<Integer, Integer> entry : unionRank.getGradeLimitAmountMap().entrySet()) {
                int gradeId = entry.getKey();
                int numHave = unionRank.getNowGradeAmountMap().getOrDefault(gradeId, 0);
                UnionProto.UnionMemberRankGradeTemp.Builder limitMsg = UnionProto.UnionMemberRankGradeTemp.newBuilder();
                limitMsg.setGradeId(gradeId);
                limitMsg.setNumLmit(entry.getValue());
                limitMsg.setNumHave(numHave);
                msg.addRankGrade(limitMsg);

                int originalNumHave = unionRank.getOriginalGradeAmountMap().getOrDefault(gradeId, 0);
                if (originalNumHave > 0) {
                    UnionProto.UnionRankGradeJoinLimitTemp.Builder gradeAmountMsg = UnionProto.UnionRankGradeJoinLimitTemp.newBuilder();
                    gradeAmountMsg.setGradeId(gradeId);
                    gradeAmountMsg.setNum(originalNumHave);
                    msg.addOriginalGradeAmount(gradeAmountMsg);
                }
            }
        }
        return msg;
    }

    public static RankProto.UnionRankListMsg.Builder parseUnionValueRankListMsg(List<UnionValueRank> unionRankList){
        RankProto.UnionRankListMsg.Builder listMsg = RankProto.UnionRankListMsg.newBuilder();
        if(null != unionRankList){
            for(UnionValueRank unionRank : unionRankList){
                RankProto.UnionRankTempMsg.Builder msg = parseUnionRankTempMsg(unionRank);
                if(null != msg){
                    listMsg.addRankList(msg);
                }
            }
        }
        return listMsg;
    }

    public static UnionActivityRankProto.UnionActivityRankTempMsg.Builder parseUnionActivityRankTempMsg(UnionActivityRank unionActivityRank){
        if(null == unionActivityRank){
            return null;
        }
        UnionActivityRankProto.UnionActivityRankTempMsg.Builder msg = UnionActivityRankProto.UnionActivityRankTempMsg.newBuilder();
        msg.setUnionUid(unionActivityRank.getUnionUid());
        UnionProto.UnionBaseTempMsg.Builder unionMsg = UnionBasePb.parseUnionBaseTempMsg(unionActivityRank.getUnionBaseInfo());
        msg.setUnionBaseData(unionMsg);
        msg.setValue(unionActivityRank.getValue().toString());
        return msg;
    }

    public static UnionActivityRankProto.UnionActivityRankListMsg.Builder parseUnionActivityRankListMsg(List<UnionActivityRank> unionActivityRankList){
        UnionActivityRankProto.UnionActivityRankListMsg.Builder listMsg = UnionActivityRankProto.UnionActivityRankListMsg.newBuilder();
        if(null != unionActivityRankList){
            for(UnionActivityRank unionActivityRank : unionActivityRankList){
                UnionActivityRankProto.UnionActivityRankTempMsg.Builder msg = parseUnionActivityRankTempMsg(unionActivityRank);
                if(null != msg){
                    listMsg.addRankList(msg);
                }
            }
        }
        return listMsg;
    }

    public static UnionActivityRankProto.UnionActivityRankListMsg.Builder parseUnionActivityRankListMsg(int rankNum,
            List<UnionActivityRank> unionActivityRankList){
        UnionActivityRankProto.UnionActivityRankListMsg.Builder listMsg = UnionActivityRankProto.UnionActivityRankListMsg.newBuilder();
        if(null != unionActivityRankList){
            int i = 0;
            for(UnionActivityRank unionActivityRank : unionActivityRankList){
                UnionActivityRankProto.UnionActivityRankTempMsg.Builder msg = parseUnionActivityRankTempMsg(unionActivityRank);
                if(null != msg){
                    listMsg.addRankList(msg);
                    i++;
                    if (i >= rankNum) {
                        break;
                    }
                }
            }
        }
        return listMsg;
    }

    public static RankProto.TopRankTemp.Builder getTopRankTemp(int rankType, RankProto.RankTempMsg msg) {
        RankProto.TopRankTemp.Builder builder = RankProto.TopRankTemp.newBuilder();
        builder.setRankTemp(msg);
        builder.setType(rankType);
        return builder;
    }

    public static UnionActivityRankProto.UnionActivityRankListMsg.Builder parseCrossUnionActivityRankListMsg(int rankNum, List<CrossActivityUnionRank> rankList) {
        UnionActivityRankProto.UnionActivityRankListMsg.Builder listMsg = UnionActivityRankProto.UnionActivityRankListMsg.newBuilder();
        if(null != rankList){
            int i = 0;
            for(CrossActivityUnionRank unionActivityRank : rankList){
                UnionActivityRankProto.UnionActivityRankTempMsg.Builder msg = parseCrossUnionActivityRankTempMsg(unionActivityRank);
                if(null != msg){
                    listMsg.addRankList(msg);
                    i++;
                    if (i >= rankNum) {
                        break;
                    }
                }
            }
        }
        return listMsg;
    }

    private static UnionActivityRankProto.UnionActivityRankTempMsg.Builder parseCrossUnionActivityRankTempMsg(CrossActivityUnionRank unionActivityRank) {
        if(null == unionActivityRank){
            return null;
        }
        UnionActivityRankProto.UnionActivityRankTempMsg.Builder msg = UnionActivityRankProto.UnionActivityRankTempMsg.newBuilder();
        msg.setUnionUid(unionActivityRank.getUnionUid());
        UnionProto.UnionBaseTempMsg.Builder unionMsg = UnionBasePb.parseUnionBaseTempMsg(unionActivityRank.getUnionBaseInfo());
        msg.setUnionBaseData(unionMsg);
        msg.setValue(unionActivityRank.getValue().toString());
        msg.setParamValue(unionActivityRank.getUnionBaseInfo().getParamValue().toString());
        return msg;
    }
}
